<template>
  <div class="barrage-container">
    <!-- 背景动画层 -->
    <div class="background-animation">
      <div class="stars"></div>
      <div class="twinkling"></div>
      <div class="clouds"></div>
    </div>
    <div id="container" ref="container">
      <!-- 弹幕容器 -->
    </div>

    <!-- 修改弹幕输入面板布局 -->
    <div class="barrage-input-panel">
      <div class="input-container">
        <div class="input-controls">
          <el-color-picker v-model="textColor" size="small" show-alpha :predefine="predefineColors" />
          <el-select 
            v-if="barrageType === 'special'"
            v-model="fontSize" 
            placeholder="字号" 
            size="small" 
            class="font-size-select"
          >
            <el-option v-for="size in fontSizes" :key="size" :label="`${size}px`" :value="size" />
          </el-select>
          <div class="barrage-type-switch">
            <div :class="['type-option', { active: barrageType === 'normal' }]" @click="barrageType = 'normal'">
              普通弹幕
            </div>
            <div :class="['type-option', { active: barrageType === 'special' }]" @click="barrageType = 'special'">
              特殊弹幕
            </div>
          </div>
        </div>
        <div class="input-row">
          <el-input v-model="inputText" placeholder="请输入弹幕内容" class="barrage-input" @keyup.enter="sendBarrage">
            <template #append>
              <el-button @click="sendBarrage">发送</el-button>
            </template>
          </el-input>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import api from '@/api/axios.js'
import Danmuku from '@/components/barrage/v_noVideo/src/index'
import { ElMessage } from 'element-plus'

const props = defineProps({
  showPanel: {
    type: Boolean,
    default: true
  }
})

const container = ref(null)
const barrageManager = ref(null)

// 控制面板数据
const opacity = ref(100)
const limit = ref(100)
const rowGap = ref(50)
const height = ref(30)
const area = ref('all')
const isShow = ref(true)
const direction = ref('right')
const isRunning = ref(true)
const minTime = ref(4)
const maxTime = ref(8)
const showNumber = ref(0)
const allNumber = ref(0)

// 弹幕输入相关
const inputText = ref('')
const textColor = ref('#FFFFFF')
const barrageType = ref('normal')

// 预定义颜色
const predefineColors = [
  '#ff0000',
  '#00ff00',
  '#0000ff',
  '#ffff00',
  '#ff00ff',
  '#00ffff',
  '#ffffff',
]

// 添加字体大小选择
const fontSize = ref(18)
const fontSizes = [14, 16, 18, 20, 24, 28, 32, 36]

// 初始化弹幕管理器
onMounted(async() => {
  const { data: { data } } = await api.getConfessionBarrages()
  barrageManager.value = Danmuku.create({
    container: container.value,
    hooks: {
      barrageCreate(barrage, node) {
        if (barrage.isSpecial) return

        if (typeof barrage.data !== 'string') {
          node.style.color = barrage.data.color
          node.textContent = barrage.data.content
        } else {
          node.textContent = barrage.data
        }

        node.classList.add('barrage')

        // 添加鼠标事件
        node.onmouseenter = () => {
          barrage.pause()
          node.classList.add('active')
        }

        node.onmouseleave = () => {
          barrage.resume()
          node.classList.remove('active')
        }

        node.onclick = () => barrage.destroy()
      }
    },
    limit: Number(limit.value),
    height: Number(height.value),
    rowGap: Number(rowGap.value),
    direction: direction.value,
    times: [Number(minTime.value), Number(maxTime.value)]
  })

  // 开始轮询
  barrageManager.value.start()

  // 发送测试弹幕
  setInterval(() => {
    const cur = data[Math.floor(Math.random() * data.length)]
    barrageManager.value.send(cur)
  }, 500)
})

onUnmounted(() => {
  if (barrageManager.value) {
    barrageManager.value.clear()
  }
})

// 发送弹幕方法
const sendBarrage = async() => {
  if (!inputText.value.trim()) return

  if (barrageType.value === 'normal') {
    // 发送普通弹幕
    barrageManager.value.send({
      content: inputText.value,
      color: textColor.value
    })
  } else {
    // 发送特殊弹幕
    barrageManager.value.sendSpecial({
      duration: 5,
      direction: 'none',
      position: () => ({
        x: Math.random() * window.innerWidth,
        y: Math.random() * window.innerHeight
      }),
      data: {
        content: inputText.value,
        color: textColor.value,
        fontSize: `${fontSize.value}px`
      },
      hooks: {
        create(barrage, node) {
          node.textContent = inputText.value
          node.style.color = textColor.value
          node.style.fontSize = `${fontSize.value}px`
          node.classList.add('special-barrage')
        }
      }
    })
  }

  await api.sendConfessionBarrage({
    content: inputText.value,
    color: textColor.value,
    type: barrageType.value,
    fontSize: parseInt(fontSize.value)
  })
  console.log({
    content: inputText.value,
    color: textColor.value,
    type: barrageType.value,
    fontSize: parseInt(fontSize.value)
  })

  ElMessage({
    message: '发送弹幕成功!',
    type: 'success',
  })

  // 清空输入
  inputText.value = ''
}

// 对外暴露方法
defineExpose({
  send: (text) => barrageManager.value?.send(text),
  sendSpecial: (config) => barrageManager.value?.sendSpecial(config),
  manager: () => barrageManager.value
})
</script>

<style scoped>
.barrage-container {
  width: 100%;
  height: 100%;
  position: relative;
  background: linear-gradient(to bottom, #0f2027, #203a43, #2c5364);
  overflow: hidden;
}

/* 星空背景 */
.background-animation {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.stars {
  background: #000 url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1231630/stars.png) repeat;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: block;
  z-index: 0;
}

.twinkling {
  width: 10000px;
  height: 100%;
  background: transparent url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1231630/twinkling.png) repeat;
  background-size: 1000px 1000px;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 1;
  animation: move-background 70s linear infinite;
}

.clouds {
  width: 10000px;
  height: 100%;
  background: transparent url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/1231630/clouds_repeat.png) repeat;
  background-size: 1000px 1000px;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 2;
  opacity: 0.4;
  animation: move-background 150s linear infinite;
}

@keyframes move-background {
  from {
    transform: translate3d(0, 0, 0);
  }

  to {
    transform: translate3d(-1000px, 0, 0);
  }
}

/* 添加渐变光晕效果 */
.barrage-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  background: radial-gradient(circle at 50% 50%,
      rgba(255, 255, 255, 0.1) 0%,
      rgba(0, 0, 0, 0.3) 100%);
  pointer-events: none;
  z-index: 3;
}

/* 修改弹幕样式以适应新背景 */
:deep(.barrage) {
  display: flex !important;
  justify-content: center;
  align-items: center;
  height: 30px;
  font-size: 18px;
  border-radius: 28px;
  color: #fff;
  padding: 0 20px;
  box-sizing: border-box;
  cursor: pointer;
  transition: all 0.3s ease;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

:deep(.active) {
  z-index: 4;
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
}

/* 确保弹幕容器在动画层上方 */
#container {
  position: relative;
  z-index: 4;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

/* 从 index.html 复制的样式 */
.operation-panel {
  position: fixed;
  width: 280px;
  top: 0;
  bottom: 0;
  left: 0;
  overflow-x: hidden;
  padding: 20px;
  z-index: 999;
  border-radius: 0 10px 10px 0;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.2);
}

.item {
  width: 100%;
  text-align: left;
  color: #fff;
  font-size: 16px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 18px;
}

/* 修改弹幕输入面板样式 */
.barrage-input-panel {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 500px;
  padding: 15px;
  background: transparent;
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1000;
}

.input-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.input-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.barrage-input {
  flex: 1;
}

.barrage-type-switch {
  display: flex;
  gap: 2px;
  background: rgba(255, 255, 255, 0.1);
  padding: 3px;
  border-radius: 20px;
  margin-left: auto;
}

.type-option {
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 14px;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.6);
  transition: all 0.3s ease;
}

.type-option:hover {
  color: rgba(255, 255, 255, 0.9);
}

.type-option.active {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 修改输入框样式 */
:deep(.el-input__inner) {
  background: rgba(255, 255, 255, 0.9) !important;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #333 !important;
}

:deep(.el-input__inner::placeholder) {
  color: #999;
}

:deep(.el-button) {
  background: #409EFF;
  border: none;
  color: #fff;
}

:deep(.el-button:hover) {
  background: #66b1ff;
}

/* 特殊弹幕样式 */
:deep(.special-barrage) {
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 0 10px currentColor;
  padding: 8px 20px;
  border-radius: 20px;
}

.font-size-select {
  width: 90px;
  transition: all 0.3s ease;
}

:deep(.el-select .el-input__inner) {
  background: rgba(255, 255, 255, 0.9) !important;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #333 !important;
}

:deep(.el-select-dropdown__item) {
  color: #333;
}

:deep(.el-select-dropdown__item.selected) {
  color: #409EFF;
  font-weight: bold;
}
</style>